package com.cinc.ebc.fi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cinc.ebc.fi.constant.AparInitialStatus;
import com.cinc.ebc.fi.entity.FiApOriginalAmount;
import com.cinc.ebc.fi.mapper.FiApOriginalAmountMapper;
import com.cinc.ebc.fi.model.FiApOriginalAmountModel;
import com.cinc.ebc.fi.service.IFiApOriginalAmountService;
import com.cinc.ebc.mm.entity.MmStockInit;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Description: 应付初始化
 * @Author: jeecg-boot
 * @Date:   2022-12-07
 * @Version: V1.0
 */
@Service
public class FiApOriginalAmountServiceImpl extends ServiceImpl<FiApOriginalAmountMapper, FiApOriginalAmount> implements IFiApOriginalAmountService {

    @Autowired
    private FiApOriginalAmountMapper fiApOriginalAmountMapper;

    @Override
    public void pageList(Page<FiApOriginalAmountModel> page, FiApOriginalAmount fiApOriginalAmount) {
        String sql = QueryGenerator.installAuthJdbc(FiApOriginalAmount.class);
        page.setRecords(fiApOriginalAmountMapper.selectModelList(page, fiApOriginalAmount, sql));
    }

    @Transactional
    @Override
    public void handleInitial() {
        LambdaQueryWrapper<FiApOriginalAmount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(FiApOriginalAmount::getStatus,AparInitialStatus.STATUS_10);
//        queryWrapper.eq(FiApOriginalAmount::getOriginalDate)
        int initCount = fiApOriginalAmountMapper.selectCount(queryWrapper);
        if(initCount > 0){
            throw new RuntimeException("初始化操作只能在初始月份执行");
        }
        List<FiApOriginalAmount> errList = fiApOriginalAmountMapper.selectCalculateErrList();
        if(oConvertUtils.listIsNotEmpty(errList)){
            throw new RuntimeException("期初余额计算有误，请先修改（单据号：" + errList.get(0).getCode() + "）");
        }
        FiApOriginalAmount fiApOriginalAmountMoidfy = new FiApOriginalAmount();
        fiApOriginalAmountMoidfy.setStatus(AparInitialStatus.STATUS_20);
        fiApOriginalAmountMapper.update(fiApOriginalAmountMoidfy,null);
    }

    @Override
    public boolean save(FiApOriginalAmount fiApOriginalAmount) {
        LambdaQueryWrapper<FiApOriginalAmount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FiApOriginalAmount::getSupplyId, fiApOriginalAmount.getSupplyId());
        int count = fiApOriginalAmountMapper.selectCount(queryWrapper);
        if(count > 0){
            throw new RuntimeException("此供应商已存在记录");
        }
        return super.save(fiApOriginalAmount);
    }

    @Override
    public boolean updateById(FiApOriginalAmount fiApOriginalAmount) {
        LambdaQueryWrapper<FiApOriginalAmount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FiApOriginalAmount::getSupplyId, fiApOriginalAmount.getSupplyId());
        queryWrapper.ne(FiApOriginalAmount::getId, fiApOriginalAmount.getId());
        int count = fiApOriginalAmountMapper.selectCount(queryWrapper);
        if(count > 0){
            throw new RuntimeException("此供应商已存在记录");
        }
        return super.updateById(fiApOriginalAmount);
    }
}
